//
//  JourneyIntroductionViewController.m
//  MyTravel
//
//  Created by Hunk on 11-5-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "JourneyIntroductionViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "NetworkManager.h"
#import "MyLoadingView.h"

#define TAG_TABLEVIEW 55

@implementation JourneyIntroductionViewController
@synthesize journeyId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    SAFE_RELEASE(arrayJourneyIntroduction)
    SAFE_RELEASE(textView)
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setTitle:@"行程简介"];
    
    arrayJourneyIntroduction = [[NSMutableArray alloc] init];
    
    // Background view
    UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    [bgImageView setImage:[[UIImage imageNamed:@"CellBackground.png"]
                           stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
    [self.view addSubview:bgImageView];
    [bgImageView release];
    
    // Border view
    UIView* borderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    [borderView setBackgroundColor:[UIColor clearColor]];
    borderView.layer.borderWidth = 2.0;
    borderView.layer.borderColor = [UIColor brownColor].CGColor;
    [self.view addSubview:borderView];
    [borderView release];
    
    // TextView
    textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    [textView setBackgroundColor:[UIColor clearColor]];
    [textView setShowsVerticalScrollIndicator:YES];
    [textView setTextColor:TEXTCOLOR];
    [textView setFont:[UIFont fontWithName:@"AmericanTypewriter" size:18]];
    [textView setTextAlignment:UITextAlignmentLeft];
    [textView setEditable:NO];
    [textView setText:@""];
    [self.view addSubview:textView];
    
//    NSArray* array1 = [NSArray arrayWithObjects:
//                       @"行程中所述机票为团体经济舱机票，含机场税；",
//                       @"ADS签证费用；",
//                       @"境外旅游巴士及专业司机；",
//                       @"专业中文导游；",
//                       @"行程中标明的欧洲星级标准酒店1/2标准间，含酒店欧陆式早餐；",
//                       @"中式午晚餐，团队以5菜1汤为标准(餐自理-游客自行准备用餐，请参照行程中标注，包括退餐) （退餐则按照每人每顿6欧元的标准来退餐)；",
//                       @"行程表中带*的景点第一门票；",
//                       @"旅游意外保险费用；", nil];
//    [arrayJourneyIntroduction addObjectsFromArray:array1];
    
    
//    // Add tableView
//    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)
//                                                          style:UITableViewStyleGrouped];
//    [tableView setDelegate:self];
//    [tableView setDataSource:self];
//    [tableView setTag:TAG_TABLEVIEW];
//    [self.view addSubview:tableView];
    
    // Data
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:journeyId, @"journeyId", nil];
    
    [[NetworkManager sharedNetworkManager] requestServerForMethod:NetworkRequest_GetJourneyIntroduction
                                                   WithParameters:parameters
                                                 AndCallBackScope:self
                                                    AndSuccessSel:@"onSuccess:"
                                                     AndFailedSel:@"onFailed:"];
    
    [[MyLoadingView sharedMyLoadingView] startLoadingAnimation];
}

/*
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [arrayJourneyIntroduction count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize size = [[arrayJourneyIntroduction objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:16]
                                                        constrainedToSize:CGSizeMake(300, 1000)
                                                            lineBreakMode:UILineBreakModeCharacterWrap];
    return (size.height > 50.0 ? size.height + 20 : 50.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryNone;
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // Get one news
    // NSDictionary* newsDictionary = [arrayJourneyIntroduction objectAtIndex:indexPath.row];
    
//    // Cell background view
//    UIView* cellBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
//    UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
//    [bgImageView setImage:[UIImage imageNamed:@"CellBackground.png"]];
//    [cellBgView addSubview:bgImageView];
//    [bgImageView release];
//    [cell setBackgroundView:cellBgView];
//    [cellBgView release];
    
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
	cell.textLabel.numberOfLines = 30;
    cell.textLabel.text = [NSString stringWithFormat:@"%d.%@", (indexPath.row + 1), [arrayJourneyIntroduction objectAtIndex:indexPath.row]];
    
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
*/

#pragma mark -
#pragma mark NetworkManager callback Methods
- (void)onSuccess:(id)sender
{
    NSDictionary* resultDic = (NSDictionary*)sender;
    
    //NSLog(@"%@\n", resultDic);
    
    [textView setText:[resultDic objectForKey:@"journeyIntroduction"]];
    
//    [arrayJourneyIntroduction removeAllObjects];
//    
//    [arrayJourneyIntroduction addObjectsFromArray:[[resultDic objectForKey:@"journeyIntroduction"] componentsSeparatedByString:@"[OVER]"]];
    
//    UITableView* tableView = (UITableView*)[self.view viewWithTag:TAG_TABLEVIEW];
//	[tableView reloadData];
}

- (void)onFailed:(id)sender
{
	NSDictionary* resultDic = (NSDictionary*)sender;
    
    // 暂无数据提示
	UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:SORRY_NODATA 
                                                        message:[resultDic objectForKey:@"message"]
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

#pragma mark scrollViewDidScroll 上拉刷新
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
